import {BrowserRouter as Router, Navigate, Route, Routes} from 'react-router-dom';
import NotFound from '@/views/NotFound';
import React from "react";
import {UseGuards} from "@/hooks/useGuards";
import Login from "@/views/Login";
import {baseRoute} from "@/routers/routes";
import {getLStorage} from "@/utils/auth";
import {LocalStorageEnum} from "@/enums/LocalStorageEnum";
import _ from 'lodash'

const RouterComponent: React.FC = () => {
    const dynamicRoutes = getLStorage(LocalStorageEnum.MENU) ? JSON.parse(getLStorage(LocalStorageEnum.MENU)) : []
    const routes = [].concat(baseRoute, dynamicRoutes)
    return (
        <Router basename={process.env.PUBLIC_URL}>
            <Routes>
                <Route path="/login" element={<Login/>}/>
                {
                    routes.map((route) => (
                        // 在每个路由内部使用 UseGuards 组件
                        route.routes && route.routes.length > 0 ?
                            <Route key={route.path} path={route.path as string}
                                   element={<UseGuards route={route.routes[0]}/>}>
                                {
                                    _.map(route.routes, child => (
                                        <Route key={child.path} path={child.path as string}
                                               element={<UseGuards route={child}/>}/>
                                    ))
                                }
                            </Route>
                            :
                            <Route key={route.path} path={route.path as string}
                                   element={<UseGuards route={route}/>}/>
                    ))
                }
                <Route path="/" element={<Navigate to='/home'/>}/>
                <Route path="*" element={<NotFound/>}/>
            </Routes>
        </Router>
    );
};

export default RouterComponent;